﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HotelMgr.Models;
using HotelMgr.ViewModels;

namespace HotelMgr.Areas.Admin.Controllers
{
    [Authorize(Roles = "Administrators")]
    public class OrderManagerController : Controller
    {
        private HotelEntities db = new HotelEntities();

        //
        // GET: /Admin/OrderManager/

        public ViewResult Index()
        {
            return View(db.Orders.ToList());
        }

        //
        // GET: /Admin/OrderManager/Details/5

        public ViewResult Details(int id)
        {
            Order order = db.Orders.Find(id);
            return View(order);
        }

        //
        // GET: /Admin/OrderManager/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Admin/OrderManager/Create

        [HttpPost]
        public ActionResult Create(Order order)
        {
            if (ModelState.IsValid)
            {
                db.Orders.Add(order);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(order);
        }
        
        //
        // GET: /Admin/OrderManager/Edit/5
 
        public ActionResult Edit(int id)
        {
            Order order = db.Orders.Find(id);
            return View(order);
        }

        //
        // POST: /Admin/OrderManager/Edit/5

        [HttpPost]
        public ActionResult Edit(Order order)
        {
            if (ModelState.IsValid)
            {
                db.Entry(order).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(order);
        }

        //
        // GET: /Admin/OrderManager/Delete/5

        public ActionResult Delete(int id)
        {
            Order order = db.Orders.Find(id);
            return View(order);
        }

        //
        // POST: /Admin/OrderManager/Delete/5

        //[HttpPost, ActionName("Delete")]
        public ActionResult OrderDelete(int id)
        {            
            var order = db.Orders.Find(id);
            db.Orders.Remove(order);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        //#region OrderGrid
        //public ActionResult Select()
        //{
        //    return View("Index", db.Orders.ToList());
        //}
        //public ActionResult Insert()
        //{
        //    Order order=new Order();
        //    if (TryUpdateModel(order))
        //    {
        //        db.Orders.Add(order);
        //        db.SaveChanges();
        //        return RedirectToAction("Index"); 
        //    }
        //    return View("Index", db.Orders.ToList());
        //}
        //public ActionResult Save(int id)
        //{
        //    Order order = new Order {OrderID = id};
        //    {
        //        db.Entry(order).State = EntityState.Modified;
        //        db.SaveChanges();
        //        return RedirectToAction("Index");
        //    }
        //    return View();
        //}
        //public ActionResult Delete()
        //{
        //    return View("Index", db.Orders.ToList());
        //}
        //#endregion
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}